package app.quiz;

import app.quiz.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 8/10/12
 * Time: 3:30 PM
 */
public class LoginServlet extends GenericPatternServlet {

    private static String username, password;
    @Override
    protected void setHtmlFileName() {
        htmlFileName = "NameForm.html";
    }

    @Override
    protected void fillInHTMLTemplate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        loadLoginCookie(request.getCookies());
        htmlTemplate.reset();
        htmlTemplate.setAttribute(Constants.USERNAME, username);
        htmlTemplate.setAttribute(Constants.PASSWORD,password);
    }



    private void loadLoginCookie(Cookie[] cookies) throws IOException {

        username = ""; password = "";

        if (cookies != null) {
            for (Cookie cookie : cookies) {

                if (cookie.getName().equals(Constants.USERNAME))
                    username = cookie.getValue();
                if (cookie.getName().equals(Constants.PASSWORD))
                    password = cookie.getValue();
            }
        }

    }
}
